// pages/tobeboss/tobeboss.js
const HTTP = require('../../utils/util');
const Common = require('../../libs/common');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cate_id: 0,
        option1: [],
        option2: [{ text: '请选择分类二', value: 0 }],
        value1: 0,
        value2: 0,
        disabled: true,
        cate1: '',
        cate2: '',
        height:0,

        name:'',
        phone:'',
        province:'',
        city:'',
        area:'',
        detail:'',
        showStatus:0
    },
    postShopInfo(){
        HTTP.postData('/api/person/becomeStore',{
            name:this.data.name,
            mobile:this.data.phone,
            cate_id:this.data.cate2,
            province:this.data.province,
            city:this.data.city,
            region:this.data.area,
            detail:this.data.detail
        },res=>{
            wx.showToast({
                icon:'none',
                title:res.msg,
                duration:2000
            });
            setTimeout(()=>{
                wx.switchTab({
                    url:'../my/my'
                });
            },2000)
        });
    },
    handleTap(e){
        let data = this.data;
        if(!data.name){
            wx.showToast({
                icon:'none',
                title:'请输入商店名称'
            });
            return false;
        }else if(!Common.checkPhone(data.phone)){
            wx.showToast({
                icon:'none',
                title:'请输入正确电话'
            });
            return false;
        }else if(!data.cate_id){
            wx.showToast({
                icon:'none',
                title:'请选择一级分类'
            });
            return false;
        }else if(!data.cate2){
            wx.showToast({
                icon:'none',
                title:'请选择二级分类'
            });
            return false;
        }else if(!data.province){
            wx.showToast({
                icon:'none',
                title:'请输入省'
            });
            return false;
        }else if(!data.city){
            wx.showToast({
                icon:'none',
                title:'请输入市'
            });
            return false;
        }else if(!data.area){
            wx.showToast({
                icon:'none',
                title:'请输入区'
            });
            return false;
        }else if(!data.detail){
            wx.showToast({
                icon:'none',
                title:'请输入详细地址'
            });
            return false;
        }else {
            console.log('happy');
            this.postShopInfo();
        }

    },
    inputChange(e) {
        let type = Number(e.target.dataset.type);
        let value = e.detail.value;
        console.log(e);
        switch (type) {
            case 1:
                this.setData({
                    name:value
                });
                break;
            case 2:
                this.setData({
                    phone:value
                });
                break;
            case 3:
                this.setData({
                    province:value
                });
                break;
            case 4:
                this.setData({
                    city:value
                });
                break;
            case 5:
                this.setData({
                    area:value
                });
                break;
            case 6:
                this.setData({
                    detail:value
                });
                break;
        };
    },
    handleCateList(e) {
        if (this.data.cate_id === 0) {
            let arr = [{ text: '请选择分类一', value: 0 }];
            e.map(item => {
                let obj = {
                    value: item.id,
                    text: item.name
                };
                arr.push(obj);
            });
            this.setData({
                option1: arr,
                disabled: true
            });
        } else {
            let arr = [{ text: '请选择分类二', value: 0 }];
            e.map(item => {
                let obj = {
                    value: item.id,
                    text: item.name
                };
                arr.push(obj);
            });
            this.setData({
                option2: arr,
                disabled: false
            });
        }
    },
    getCateList(callback) {
        HTTP.getData('/api/person/business', {
            cate_id: this.data.cate_id
        }, res => {
            if (this.data.cate_id === 0) {
                this.handleCateList(res.data.first_cate);
                callback();
            } else {
                this.handleCateList(res.data.second_cate);
                callback();
            }
        });
    },
    handleChange1(e) {
        if (e.detail === 0) {
            this.setData({
                disabled: true,
                option2: [{ text: '请选择分类二', value: 0 }],
                value2: 0,
                cate2: 0,
                cate_id: 0
            });
        } else {
            this.setData({
                cate_id: e.detail
            })
            this.getCateList(() => {
                this.setData({
                    disabled: false,
                    value2: 0,
                    cate2: 0
                });
            })
        }
    },
    handleChange2(e) {
        this.setData({
            cate2: e.detail
        });
        console.log(this.data.cate_id, this.data.cate2)
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let is_shop = +options.is_shop;
        if(is_shop === 0){
            this.setData({
                height:wx.getSystemInfoSync().windowHeight-262,
                showStatus:0
            });
        }else if(is_shop === 1){
            this.setData({
                height:wx.getSystemInfoSync().windowHeight-262,
                showStatus:0
            });
            wx.showModal({
                // title: '提示',
                showCancel:false,
                content: '商铺信息已提交，等待平台审核',
                success (res) {
                  if (res.confirm) {
                    wx.navigateBack();
                  }
                }
              })
        }else if(is_shop === 2) {
            this.setData({
                showStatus:2
            });
        }else if(is_shop === 3){
            this.setData({
                height:wx.getSystemInfoSync().windowHeight-262,
                showStatus:0
            });
            wx.showModal({
                showCancel:false,
                content: '商铺信息审核失败，请重新提交！',
                success (res) {
                  if (res.confirm) {
                    
                  }
                }
              })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getCateList(() => { });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})